#!/bin/sh
#
#
# Copyright (c)2004 DuckCorp(tm)
#
#
#
# This file is part of EMIR 
#
# EMIR is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# EMIR is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with EMIR  if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

CONF="/etc/emir.conf"
LOG="/var/lib/emir/conf_check.log"
SCRIPTDIR="/usr/share/emir/"
SCRIPTDIR2="/var/lib/emir/"

PHP="/usr/bin/php4"
IPTS="/sbin/iptables-save"
XMLLINT="/usr/bin/xmllint"

#--------------------------------------------------------------------------

PROGNAME="EMIR"
CMDNAME="emir-ctrl"
VERSION="0.1.0"

msg_version() {
	cat <<EOF
$PROGNAME V$VERSION
Copyright (c)2004 DuckCorp(tm)

EMIR is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

EMIR is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with EMIR; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

EOF
}

msg_help() {
	cat <<EOF
Usage : $CMDNAME [OPTION | COMMAND]
Control Internet configuration with EMIR.

Options :
        -h --help    Display this help message
        -v --version Display the version number

See manpage for available commands.

EOF
}

status ()
{
	Z=$($IPTS 2>/dev/null | grep emir_configuration)
	if [[ $Z  ]]
	then
		return 0
	else
		return 1
	fi
}

check ()
{
	Z=$($XMLLINT --valid --noblanks --noout $CONF 2>&1)
	if [[ $Z ]]
	then
		echo "EMIR configuration is BAD ! (see '$LOG' for more information)"
		echo "$Z" >$LOG
		exit
	else
		echo "EMIR configuration is OK !"
		echo "All is OK !" > $LOG
	fi
}

gen ()
{
	echo -n "Generating Configuration for EMIR..."
	$PHP -q $SCRIPTDIR/emir_gen.php
	echo "done"
}

start ()
{
	echo -n "Starting EMIR..."
	$SCRIPTDIR2/emir_start.sh
	echo "done"
}

stop ()
{
	echo -n "Stoping EMIR..."
	$SCRIPTDIR2/emir_stop.sh
	echo "done"
}



case "$1" in
  --help|-h)
    msg_help
	exit 0
	;;
  --version|-v)
    msg_version
	exit 0
	;;
  start)
  	if status
	then
		echo "EMIR already started."
	else
		check
		gen
  		start
	fi
	;;
  stop)
  	if status
	then
  		stop
	else
		echo "EMIR already stoped."
	fi
	;;
  restart)
  	if status
	then
		check
		gen
  		stop
		start
	else
		echo "EMIR not started."
		check
		gen
		start
	fi
	;;
  status)
  	if status
	then
		echo "EMIR started."
	else
		echo "EMIR stoped."
	fi
	;;
  gen)
	check
  	gen
	;;
  check)
  	check
	;;
  *)
    ;;
esac

